package com.sky.aspect;

import com.sky.annotaion.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

//自定义切片类，用于实现自动填充功能
@Aspect
@Slf4j
@Component
public class AutoFillAspect {
    //切入点
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotaion.AutoFill)")
    public void autoFillPointCut(){}

    //前置通知，在通知中进行公共字段赋值
    //以及连接点获取执行的方法名、参数等信息
    @Before("autoFillPointCut()")
    public void autoFill (JoinPoint joinPoint) {
        log.info("开始进行公共字段自动填充");

        /*获取到当前拦截的方法上的数据库操作类型*/
        //获取方法签名对象
         MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
         //获取方法上的注解对象
         AutoFill autoFill = methodSignature.getMethod().getAnnotation(AutoFill.class);
         //获取数据库操作类型
         OperationType operationType = autoFill.value();

         /*获取到当前拦截的方法的参数*/
         Object[] args = joinPoint.getArgs();
         if (args  == null  || args.length == 0) {
             return;
         }
         //获取第一个参数，默认第一个参数是实体对象
          Object object = args[0];

         /*根据对应的数据库操作类型，为对应的字段赋值,通过反射*/
        //准备相应数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        // INSERT
          if (operationType == OperationType.INSERT) {
              try {
                  log.info("公共字段赋值");
                  //通过反射获取到实体类中的属性
                  Method setUpdateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                  Method setCreateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                  Method setUpdateUser = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                  Method setCreateUser = object.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                  //通过反射设置值
                  setUpdateTime.invoke(object, now);
                  setCreateTime.invoke(object, now);
                  setUpdateUser.invoke(object, currentId);
                  setCreateUser.invoke(object, currentId);
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
          // UPDATE
        else if (operationType == OperationType.UPDATE) {
            try {
                log.info("公共字段赋值");
                Method setUpdateTime = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = object.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(object, now);
                setUpdateUser.invoke(object, currentId);
            }
            catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
